package com.yst.gateway.monitor.services.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yst.gateway.monitor.common.utils.DateUtils;
import com.yst.gateway.monitor.common.utils.LoginUserCache;
import com.yst.gateway.monitor.common.utils.UUIDGenerator;
import com.yst.gateway.monitor.dao.QzDictionaryMapper;
import com.yst.gateway.monitor.models.QzDictionary;
import com.yst.gateway.monitor.services.QzDictionaryService;


/**
 * 前置系统字典管理实现类
 * 
 * @author Administrator
 *
 */
@Service
@Scope("prototype")
public class QzDictionaryServiceImpl implements QzDictionaryService {

	@Autowired
	private QzDictionaryMapper qzDictionaryMapper;
	
	
	@Override
	public PageInfo<QzDictionary> getByPage(int pageNum, int pageSize,
			Map<String, Object> paramMap) {
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<QzDictionary>(qzDictionaryMapper.selectAll(paramMap));
	}

	@Override
	public boolean add(QzDictionary t) {
		boolean flag = true;
		t.setDictionaryId(UUIDGenerator.getUUID());
		t.setPid("0");
		t.setCtime(DateUtils.getNowTime());
		t.setCreator(LoginUserCache.getUser().getSysUserName());
		t.setStatus(1);
		if (qzDictionaryMapper.insert(t) > 0) {
			
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean update(QzDictionary t) {
		boolean flag = true;
		t.setUtime(DateUtils.getNowTime());
		t.setModifier(LoginUserCache.getUser().getSysUserName());
		if (qzDictionaryMapper.updateById(t) > 0) {
			
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public QzDictionary getById(String id) {
		return qzDictionaryMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<QzDictionary> getByMap(Map<String, Object> paramMap) {
		return qzDictionaryMapper.selectAll(paramMap);
	}


	@Override
	public boolean delById(String id) {
		boolean flag = true;
		if (qzDictionaryMapper.deleteByPrimaryKey(id) > 0) {
			
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public List<QzDictionary> getByCode(String code) {
		return qzDictionaryMapper.getByCode(code);
	}

	@Override
	public boolean updateStatusById(String id) {
		boolean flag = true;
		QzDictionary qzDictionary = getById(id);
		if (qzDictionary != null) {
			qzDictionary.setStatus(0);
			qzDictionary.setUtime(DateUtils.getNowTime());
			qzDictionary.setModifier(LoginUserCache.getUser().getSysUserName());
		}
		if (qzDictionaryMapper.updateByPrimaryKey(qzDictionary) > 0) {
			
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean updateStatusByPid (String id) {
		boolean flag = true;
		QzDictionary qz =getById(id);
		if (qz != null) {
			QzDictionary qzDictionary = new QzDictionary();
			qzDictionary.setDictionaryId(id);
			qzDictionary.setStatus(qz.getStatus()==1?2:1);			
			qzDictionary.setUtime(DateUtils.getNowTime());
			flag=qzDictionaryMapper.updateStatusByPid(qzDictionary)>0?true:false; 
		}
		
		return flag;
	}
	
	
	
	@Override
	public boolean lockById (String id) {
		boolean flag = true;
		QzDictionary qz =getById(id);
		String pid = qz.getPid();
		if (qz != null) {
			QzDictionary qzDictionary = new QzDictionary();
			qzDictionary.setDictionaryId(id);
			qzDictionary.setStatus(qz.getStatus()==1?2:1);			
			qzDictionary.setUtime(DateUtils.getNowTime());
			if (qz.getStatus()==2) {
				QzDictionary sb = getById(pid);
				sb.setStatus(1);
				qzDictionaryMapper.updateStatusByPid(sb);
			}
			
			flag=qzDictionaryMapper.updateStatusByPid(qzDictionary)>0?true:false; 
		}
		
		return flag;
	}
	
	
	@Override
	public List<QzDictionary> getByPid(String id) {
		QzDictionary qz = getById(id);
		if (qz.getStatus()==2) {
			return null;
		}
		return qzDictionaryMapper.getByPid(id);
	}

	@Override
	public boolean insertByPid(QzDictionary dictionary) {
		boolean flag = true;
		dictionary.setDictionaryId(UUIDGenerator.getUUID());
		dictionary.setCreator(LoginUserCache.getUser().getSysUserName());
		dictionary.setCtime(DateUtils.getNowTime());
		dictionary.setStatus(1);
		if (qzDictionaryMapper.insertByPid(dictionary) > 0) {
			
		} else {
			flag = false;
		}
		
		return flag;
	}

	@Override
	public List<QzDictionary> getByInterId(String id) {
		return qzDictionaryMapper.getByInterId(id);
	}

	@Override
	public List<QzDictionary> selectByName(String name) {
		return qzDictionaryMapper.selectByName(name);
	}

	@Override
	public List<QzDictionary> selectByCode(String code) {
		return qzDictionaryMapper.selectByCode(code);
	}

	
	public List<QzDictionary> selectByCodeAndPid(String code,String pid) {
		return qzDictionaryMapper.selectByCodeAndPid(code, pid);
	}

}
